<?php
namespace src\logic\workflow;
use classlib\core as C;
use classlib\logger;
use classlib\input;
class doc_basic {	
	/**
	 * 魔术方法，请求的方法不存在的时候调用
	 * @param unknown $name
	 * @param unknown $args
	 */
	public function __call($name, $args) {
		$msg = sprintf('workflow:%s --- args:%s', $name, serialize($args));
		logger::inst()->write($msg);
		return 1;
	}
	
	/**
	 * 判断一个http请求的提交请求还是获取信息的请求
	 * @return string
	 */
	protected function method() {
		$method = 'get';
		if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
			$method = 'post';
		}
		return $method;
	}
	
	/**
	 * 生成一个流程实例
	 * @param array $workflow
	 * @param int  $userid
	 * @param number $pid
	 * @param number $overid
	 * @return int
	 */
	public function create(&$workflow, $userid, $pid=0, $overid=0) {
		$disname = doc_executor::disname($userid);
		$data    = array('workflowid'=>$workflow['id'], 'pid'=>$pid, 'overid'=>$overid,
			'optime'=>C\config::$nowtime, 'status'=>1, 'applyuserid'=>$userid,
			'applyusername'=>$disname, 'stime'=>C\config::$nowtime);
		$executorid = C\model::load('main.proc.execution')->new_one($data);
		//释放首个节点信息的提交
		$where   = array('workflowid'=>$workflow['id'], 'preid'=>0);
		$node    = C\model::load('main.proc.node')->get_item($where);
		$expire  = C\config::$nowtime + $node['expire'] * 3600;
		$data    = array('workflowid'=>$workflow['id'], 'executionid'=>$executorid, 'nodeid'=>$node['id'],
			'status'=>1, 'acceptuserid'=>$userid, 'acceptusername'=>$disname, 
			'accepttime'=>C\config::$nowtime, 'createtime'=>C\config::$nowtime,
			'lastmodify'=>C\config::$nowtime, 'expire'=>$expire
		);
		C\model::load('main.proc.nodestate')->new_one($data);
		return $executorid;
	}
	
	/**
	 * 驱动下一个节点信息 通过前置任务查找
	 * @param array $state
	 * @return int
	 */
	public function nextnode(&$state) {
		$norm  = C\model::load('main.proc.node');
		$where = array('workflowid'=>$state['workflowid'], 
			'result'=>$state['result'], 'preid'=>$state['nodeid']);
		$node  = $norm->get_item($where);
		if (!$node) {
			return doc_executor::EXEC_COMPLETE;
		}
		//工作指派节点
		$acceptuid = (int)input::post('acceptuid');
		$assigner  = doc_executor::disname($state['acceptuserid']);
		//验证释放已经解锁释放过了
		$sorm  = C\model::load('main.proc.nodestate');
		$snode = $sorm->get_item(array('executionid'=>$state['executionid'], 'nodeid'=>$node['id']));
		if ($snode) {
			$fields = array('status'=>1, 'assignuser'=>$assigner, 'acceptuid'=>$acceptuid);
			$sorm->save($snode['id'], $fields);//重新提交next结点重新审核
			return doc_executor::EXEC_SUCCESS;
		}
		$expire= C\config::$nowtime + $node['expire'] * 3600;
		$data  = array('workflowid'=>$state['workflowid'], 'executionid'=>$state['executionid'],
			'nodeid'=>$node['id'], 'status'=>0, 'createtime'=>C\config::$nowtime, 'acceptuid'=>$acceptuid,
			'lastmodify'=>C\config::$nowtime, 'expire'=>$expire, 'assignuser'=>$assigner
		);
		$sorm->new_one($data);
		return doc_executor::EXEC_SUCCESS;
	}
	
	/**
	 * 回退的前一个节点
	 * @param array $nodestate
	 */
	public function backnode(&$state) {
		$norm  = C\model::load('main.proc.node');
		$node  = $norm->get_one($state['nodeid']);
		$field = array('status'=>doc_executor::WAITPROCESS, 'completetime'=>0);
		$where = array('executionid'=>$state['executionid'], 'nodeid'=>$node['preid']);
		C\model::load('main.proc.nodestate')->multi_save($where, $field);
	}
	
	/**
	 * 判断节点是否可以接受
	 * @param int $userid
	 * @param int $roleid
	 * @param array $state
	 * @return bool
	 */
	public function isaccpetable($userid, $roleid, &$state) {
		if ($state['status'] != 0) {//已经被他人认领
			return false;
		}
		switch ($roleid) {
			case doc_executor::ASSIGN_USER://任务指派分组
				if ($state['acceptuid'] == $userid) {
					return true;
				}
			case doc_executor::DEPT_LEADER://申请方部门领导审批
			case doc_executor::MAIN_LEADER://申请方分管副总审批
				$execution = C\model::load('main.proc.execution')->get_one($state['executionid']);
				$user      = C\model::load('main.sys.user')->get_one($execution['applyuserid']);
				$org       = C\model::load('main.sys.organize')->get_one($user['orgid']);
				if (($roleid == doc_executor::DEPT_LEADER && $org['chargeuid'] == $userid) || 
					($roleid == doc_executor::MAIN_LEADER && $org['prechargeuid'] == $userid)) {
					return true;
				}
				break;
			case doc_executor::APPLY_USER://申请人验收
				$execution = C\model::load('main.proc.execution')->get_one($state['executionid']);
				if ($execution['applyuserid'] == $userid) {
					return true;
				}
				break;
			default:
				$where = array('userid'=>$userid);
				$roles = C\model::load('main.sys.ruser')->get_column(0, -1, $where, 'ruleid');
				if (in_array($roleid, $roles)) {
					return true;
				}
		}
		return false;
	}
	
	/**
	 * 是否可终止
	 * @param unknown $executorid
	 */
	public function isstopable($executorid) {
		return true;
	}
}

